#if defined _td_menus_included
	#endinput
#endif

#define _td_menus_included

/*
	This method shows dealers menu
*/
stock td_dealers_menu(id){
	new text[64];
	new len = charsmax(text);
	td_get_string(id, sDealersMenuTitle, text, len);
	
	new menu = menu_create(text, "dealers_menu_handler");
	
	new info[1];
	new count = 0;
	for(new dealer =0;dealer<MAX_DEALERS;dealer++) {
		if(!gDealerDef[dealer][Di_Registered])
			continue;
		if(playerAvailableDealers[id][dealer] &&   td_get_dealers_items_count( dealer) > 0){
			info[0] = dealer;
			menu_additem(menu, gDealerDef[dealer][Di_Name], info);
			count ++;
		}
	}
	switch(count) {
		case 0 : {
			td_get_string(id, sNoAccessToDealer, text, len);
	
			client_print(id, print_chat, "%s %s", prefix_disp, text);
		}
		case 1: {
			menu_destroy(menu); 
			td_dealer_item_menu(id,  info[0]);
		}
		default: menu_display(id, menu);
	}
}

public dealers_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	menu_destroy(menu);
	td_dealer_item_menu(id, info[0]);
	return;
}

stock td_dealer_item_menu(id, dealer){
	playerDealer[id] = dealer;
	new text[64];
	new len = charsmax(text);
	formatex(text,  len, "%s", gDealerDef[dealer][Di_Name]);
	new menu = menu_create(text, "dealer_item_menu_handler");
	new info[1];
	new itemName[64];
	new itemNameLen = charsmax(itemName);
	new countName[32];
	td_get_string(id, sCount, countName, charsmax(countName));
	new costName[32];
	td_get_string(id, sCount, costName, charsmax(costName));
	for(new item =0;item<Items;item++) {
		if(playerAvailableItems[id][item]){
			text[0] = '^0';
			td_get_item_name(id, item ,itemName, itemNameLen)
			formatex(text, len, "%s [%s: %d] %s: %d$",itemName, countName, gDealersItems[dealer][item][Dii_Count], costName, gDealersItems[dealer][item][Dii_Cost]);
			info[0] = item;
			menu_additem(menu, text, info);
		}
	}
	menu_display(id, menu);	
}

public dealer_item_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		td_dealers_menu(id);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	menu_destroy(menu);
	new itm = info[0];
	
	td_give_item_from_dealer(id, itm, playerDealer[id]);
	
	td_dealer_item_menu(id, playerDealer[id]);
	return;
}


stock td_item_menu(id){
	new text[64];
	new len = charsmax(text);
	td_get_string(id, sItemMenuTitle, text, len);
	new menu = menu_create(text, "item_menu_handler");
	new info[1];
	new itemName[64];
	new itemNameLen = charsmax(itemName);
	for(new item=0;item<Items;item++){
		if(playerAvailableItems[id][item] && td_any_dealer_have_item(id, item)){
			text[0] = '^0';
			td_get_item_name(id, item ,itemName, itemNameLen)
			info[0] = item;
			menu_additem(menu, itemName, info);
		}
	}
	menu_display(id, menu);
}


public item_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	menu_destroy(menu);	
	td_item_dealer_menu(id, info[0]);		
}

stock td_item_dealer_menu(id, item){
	new text[64];
	new len = charsmax(text);
	td_get_item_name(id, item, text, len);
	
	new menu = menu_create(text, "item_dealer_menu_handler");
	new info[1];
	playerItem[id] = item;
	new count ;
	new cost;
	
	new countName[32];
	td_get_string(id, sCount, countName, charsmax(countName));
	new costName[32];
	td_get_string(id, sCount, costName, charsmax(costName));
	
	for(new dealer =0;dealer<MAX_DEALERS;dealer++){
		if(!gDealerDef[dealer][Di_Registered])
			continue;
		count = gDealersItems[dealer][item][Dii_Count];
		cost = gDealersItems[dealer][item][Dii_Cost];
		if(playerAvailableDealers[id][dealer] && count >0) {
			info[0] = dealer;
			text[0] = '^0';
			formatex(text, len, "%s [%s: %d] %s: %d$", gDealerDef[dealer][Di_Name], countName, count, costName, cost);
			menu_additem(menu, text, info);
		}
	}
	menu_display(id, menu);
}

public item_dealer_menu_handler(id, menu, item){
	if(item == MENU_EXIT) {
		menu_destroy(menu);
		td_item_menu(id);
		return;
	}
	new info[1];
	new acc, call;
	menu_item_getinfo(menu, item, acc, info, 1, _, _ , call);
	if(td_give_item_from_dealer(id, playerItem[id], info[0], false)){
		td_item_menu(id);
	} else {
		td_item_dealer_menu(id, playerItem[id]);
	}
}
